/**
 * Copyright (c) 2012 Fujian FUNO Mobile Communication Technology Co., Ltd.
 * All rights reserved by FUNO.
 */
package com.funo.aspect;

import java.util.Date;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.ProceedingJoinPoint;

import com.funo.util.LogUtils;

/**
 * 
 * <hr/>
 * <b>@date 2012-12-7:</b> 新建
 * <hr/>
 * 
 * @author linzhiwei
 * @version 1.0
 */
public class BaseLogAroundAspectj {

	protected final Log log = LogFactory.getLog(getClass());

	protected Object proceed(ProceedingJoinPoint pjp) throws Throwable {
		String className = pjp.getThis().toString();
		String methodName = pjp.getSignature().getName();

		this.beforeMethod(className, methodName, pjp.getArgs());
		Object result = pjp.proceed();
		this.afterMethod(className, methodName, result);
		return result;
	}

	private void beforeMethod(String className, String methodName, Object[] args) {
		if (log.isInfoEnabled()) {
			log.info(LogUtils.handleMsg("开始执行{}.{}方法", className, methodName));
			if (null != args && args.length > 0) {
				final StringBuilder sb = new StringBuilder("参数是: {");
				for (Object argument : args) {
					String result = "";
					if (argument instanceof String || argument instanceof Integer || argument instanceof Long
							|| argument instanceof Double || argument instanceof Short || argument instanceof Float
							|| argument instanceof Byte || argument instanceof Character || argument instanceof Date) {
						result = argument.toString();
					} else {
						result = ToStringBuilder.reflectionToString(argument);
					}
					sb.append(result).append(",");
				}
				sb.setCharAt(sb.length() - 1, '}');
				log.info(sb.toString());
			}
		}
	}

	private void afterMethod(String className, String methodName, Object retVal) {
		String result = "";
		if (retVal instanceof String || retVal instanceof Integer || retVal instanceof Double || retVal instanceof Long
				|| retVal instanceof Float || retVal instanceof Byte || retVal instanceof Short
				|| retVal instanceof Character || retVal instanceof Date) {
			result = retVal.toString();
		} else {
			result = ToStringBuilder.reflectionToString(retVal);
		}
		if (log.isInfoEnabled()) {
			log.info(LogUtils.handleMsg("执行{}.{}的方法结束，返回值：{}", className, methodName, result));
		}

	}

	public static void main(String[] args) {
		Object s = "qqq";
		System.out.println(s instanceof String);
	}
}
